package com.wei.czz.framework.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.blog.dao.BlogContentDao;
import com.wei.czz.framework.blog.entity.BlogContentEntity;
import com.wei.czz.framework.blog.service.BlogContentService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2021-03-15 10:31:23
 * className: BlogContentServiceImpl 博客正文操作请求服务接口实现类
 * version: 1.0
 * description:
 */
@Service("blogContentService")
public class BlogContentServiceImpl extends ServiceImpl<BlogContentDao, BlogContentEntity>
        implements BlogContentService {

    private static final Logger log = LoggerFactory.getLogger(BlogContentServiceImpl.class);

    @Override
    public void saveOrEdit(BlogContentEntity blogContent) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername(StringUtils.EMPTY);
        Long userId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        if (Objects.isNull(blogContent.getId())) {
            blogContent.setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setUpdateUser(username)
                    .setUpdateUserId(userId)
                    .setCreateTime(date)
                    .setCreateUser(username)
                    .setCreateUserId(userId);

            int count = baseMapper.insert(blogContent);
            log.info("保存博客正文完成。count={}", count);
        } else {
            blogContent.setUpdateTime(date)
                    .setUpdateUser(username)
                    .setUpdateUserId(userId);

            int count = baseMapper.updateById(blogContent);
            log.info("修改博客正文完成。count={}", count);
        }
    }

    @Override
    public String getBlogContentById(Long blogId) {
        return baseMapper.getBlogContentById(blogId);
    }

    @Override
    public BlogContentEntity find(Long blogId, Integer version) {

        LambdaQueryWrapper<BlogContentEntity> blogContentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogContentLambdaQueryWrapper.eq(BlogContentEntity::getBlogId, blogId)
                .eq(BlogContentEntity::getVersion, version)
                .eq(BlogContentEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 第一条
        blogContentLambdaQueryWrapper.last("limit 1");
        // 查询
        List<BlogContentEntity> blogContentList = baseMapper.selectList(blogContentLambdaQueryWrapper);
        log.info("查询博客正文完成。list.size={}", blogContentList.size());
        if (CollectionUtils.isEmpty(blogContentList)) {
            throw new CzzException(ResultEnum.NOT_FOUND.getCode(), "博客正文不存在，请确认");
        }
        return blogContentList.get(0);
    }

    @Override
    public void delete(Long blogId, Integer version) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<BlogContentEntity> blogContentLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置值
        blogContentLambdaUpdateWrapper.set(BlogContentEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                        .set(BlogContentEntity::getUpdateTime, date)
                        .set(BlogContentEntity::getUpdateUser, userPo.getUsername())
                        .set(BlogContentEntity::getUpdateUserId, userPo.getUserId());
        // 规则匹配
        blogContentLambdaUpdateWrapper.eq(BlogContentEntity::getBlogId, blogId)
               .eq(BlogContentEntity::getVersion, version)
               .eq(BlogContentEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 更新
        int count = baseMapper.update(blogContentLambdaUpdateWrapper);
        log.info("删除博客正文完成。count={}", count);
    }

}